//
//  TvOSCardView.swift
//  TvOSCardView
//
//  Created by David on 2017/11/13.
//  Copyright © 2017年 david.com. All rights reserved.
//

import UIKit

private let KMaxAngle: CGFloat = CGFloat(Double.pi / 9)

class TvOSCardView: UIView {
    
    var iconView: UIImageView!
    var snowView: UIImageView!
    
   
    init() {
        super.init(frame: CGRect.zero)
//        setupUI()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
//        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        setupUI()
    }
    
    func setupUI() {
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 10)
        self.layer.shadowRadius = 10
        self.layer.shadowOpacity = 0.3
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction))
        addGestureRecognizer(pan)
        
        iconView = UIImageView(image: #imageLiteral(resourceName: "interstellar3"))
        iconView.layer.cornerRadius = 10
        iconView.clipsToBounds = true
        addSubview(iconView)
        
        snowView = UIImageView(image: #imageLiteral(resourceName: "5"))
        snowView.layer.transform = CATransform3DMakeTranslation(0, 0, 200.0)
        insertSubview(snowView, aboveSubview: iconView)
        
        iconView.frame = bounds
        snowView.frame = bounds
    }
   
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        iconView.frame = bounds
//        snowView.frame = bounds
    }
}

extension TvOSCardView {
    
    @objc func panAction(gesture: UIPanGestureRecognizer) {
        
        let location = gesture.location(in: self)
        
        switch gesture.state {
        case .changed:
            
            let halfw = bounds.width * 0.5
            let halfh = bounds.height * 0.5
            // 转换坐标系到中心点
            let dx = min(1, max(-1, (location.x - halfw) / halfw))
            let dy = min(1, max(-1, (halfh - location.y) / halfh))
            
            var t = CATransform3DIdentity
            t.m34 = 1.0 / -500
            t = CATransform3DRotate(t, KMaxAngle * dx, 0, 1, 0)
            t = CATransform3DRotate(t, KMaxAngle * dy, 1, 0, 0)
            iconView.layer.transform = t
            
            
            var t2 = CATransform3DIdentity
            t2.m34 = 1.0 / -250
            t2 = CATransform3DRotate(t, KMaxAngle * dx, 0, 1, 0)
            t2 = CATransform3DRotate(t, KMaxAngle * dy, 1, 0, 0)
            snowView.layer.transform = t
            
        case .ended,
             .cancelled:
            
            UIView.animate(withDuration: 0.3, animations: {
                self.iconView.layer.transform = CATransform3DIdentity
                self.snowView.layer.transform = CATransform3DIdentity
            })
        default:
            break
        }
    }
}








